package MultiThread;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {



    private static CountDownLatch countDownLatch = new CountDownLatch(5);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            System.out.println("计数器:"+countDownLatch.getCount());
            new Thread(new Worker1(countDownLatch,i)).start();
        }
        //等所有线程执行完， countDownLatch 到0，往下走
        countDownLatch.await();
        System.out.println("等待结束");
    }
}

class Worker1 implements Runnable{

    private CountDownLatch c;
    private int i;

    public Worker1(CountDownLatch c, int i) {
        this.c = c;
        this.i = i;
    }

    @Override
    public void run() {
        System.out.println("线程"+i + "减一");
        System.out.println("线程"+i + "，剩余数："+c.getCount());

        c.countDown();
        System.out.println("线程"+i + "，剩余数："+c.getCount());
    }
}
